package com.example.ddd.domain.service;

import com.example.ddd.domain.model.entity.Product;
import com.example.ddd.domain.repository.ProductRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ProductDomainService {
    private final ProductRepository productRepository;
    
    public Product createProduct(Product product) {
        // 领域逻辑验证
        if (product.getPrice().doubleValue() <= 0) {
            throw new IllegalArgumentException("商品价格必须大于0");
        }
        
        if (product.getStock() < 0) {
            throw new IllegalArgumentException("商品库存不能为负数");
        }
        
        return productRepository.save(product);
    }
    
    public boolean checkStock(Long productId, int quantity) {
        return productRepository.findById(productId)
                .map(product -> product.getStock() >= quantity)
                .orElse(false);
    }
}